package com.admin.exam.entity;


import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

/**
 * @author : alex
 * @date : 2020/7/29
 */
public class QuestionImport {

    @ExcelProperty(index = 0)
    private String content;
    @ExcelProperty(index = 1)
    private String answer1;
    @ExcelProperty(index = 2)
    private String answer2;
    @ExcelProperty(index = 3)
    private String answer3;
    @ExcelProperty(index = 4)
    private String answer4;
    @ExcelProperty(index = 5)
    private String answer5;
    @ExcelProperty(index = 6)
    private String answer6;
    @ExcelProperty(index = 7)
    private String correct;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getAnswer1() {
        return answer1;
    }

    public void setAnswer1(String answer1) {
        this.answer1 = answer1;
    }

    public String getAnswer2() {
        return answer2;
    }

    public void setAnswer2(String answer2) {
        this.answer2 = answer2;
    }

    public String getAnswer3() {
        return answer3;
    }

    public void setAnswer3(String answer3) {
        this.answer3 = answer3;
    }

    public String getAnswer4() {
        return answer4;
    }

    public void setAnswer4(String answer4) {
        this.answer4 = answer4;
    }

    public String getAnswer5() {
        return answer5;
    }

    public void setAnswer5(String answer5) {
        this.answer5 = answer5;
    }

    public String getAnswer6() {
        return answer6;
    }

    public void setAnswer6(String answer6) {
        this.answer6 = answer6;
    }

    public String getCorrect() {
        return correct;
    }

    public void setCorrect(String correct) {
        this.correct = correct;
    }

    public JSONArray getAnswers() {
        JSONArray answers = new JSONArray();
        Integer i = 0;
        if (StringUtils.isNotEmpty(answer1)) {
            i++;
            putJSON(answers, i, answer1.trim(), true);
        }
        if (StringUtils.isNotEmpty(answer2)) {
            i++;
            putJSON(answers, i, answer2.trim(), false);
        }
        if (StringUtils.isNotEmpty(answer3)) {
            i++;
            putJSON(answers, i, answer3.trim(), false);
        }
        if (StringUtils.isNotEmpty(answer4)) {
            i++;
            putJSON(answers, i, answer4.trim(), false);
        }
        if (StringUtils.isNotEmpty(answer5)) {
            i++;
            putJSON(answers, i, answer5.trim(), false);
        }
        if (StringUtils.isNotEmpty(answer6)) {
            i++;
            putJSON(answers, i, answer6.trim(), false);
        }
        return answers;
    }

    public JSONArray getAnswers2() {
        JSONArray answers = new JSONArray();
        Integer i = 0;
        if (StringUtils.isNotEmpty(answer1)) {
            i++;
            putJSON(answers, i, answer1.trim(), matchCorrect(i.toString(),correct));
        }
        if (StringUtils.isNotEmpty(answer2)) {
            i++;
            putJSON(answers, i, answer2.trim(), matchCorrect(i.toString(),correct));
        }
        if (StringUtils.isNotEmpty(answer3)) {
            i++;
            putJSON(answers, i, answer3.trim(), matchCorrect(i.toString(),correct));
        }
        if (StringUtils.isNotEmpty(answer4)) {
            i++;
            putJSON(answers, i, answer4.trim(), matchCorrect(i.toString(),correct));
        }
        if (StringUtils.isNotEmpty(answer5)) {
            i++;
            putJSON(answers, i, answer5.trim(), matchCorrect(i.toString(),correct));
        }
        if (StringUtils.isNotEmpty(answer6)) {
            i++;
            putJSON(answers, i, answer6.trim(), matchCorrect(i.toString(),correct));
        }
        return answers;
    }

    public boolean matchCorrect(String no, String contentStr) {
        return contentStr.contains(no);
    }

    private void putJSON(JSONArray answers, Integer no, String title, boolean correct) {
        JSONObject obj = new JSONObject();
        obj.put("no", String.valueOf(no));
        obj.put("title", title);
        obj.put("correct", correct);
        answers.add(obj);
    }

    @Override
    public String toString() {
        return "QuestionImport{" +
                "content='" + content + '\'' +
                ", answer1='" + answer1 + '\'' +
                ", answer2='" + answer2 + '\'' +
                ", answer3='" + answer3 + '\'' +
                ", answer4='" + answer4 + '\'' +
                ", answer5='" + answer5 + '\'' +
                ", answer6='" + answer6 + '\'' +
                '}';
    }
}
